package br.com.ufpr.sgp.components;

import java.util.Iterator;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.RefreshingView;
import org.apache.wicket.model.IModel;

import br.com.ufpr.util.base.BasePage;

public class MenuItemSubmenuPanel extends Panel {
    private static final long serialVersionUID = -5267338513654306745L;

    public MenuItemSubmenuPanel(final String id, final MenuItem mi, final Class<? extends BasePage> paginaAtiva) {
        super(id);
        setRenderBodyOnly(true);

        add(new Label("submenuLabel", mi.getLnkLabel()).setRenderBodyOnly(true));

        final RefreshingView<MenuItem> view = new RefreshingView<MenuItem>("submenuList") {
            private static final long serialVersionUID = 6697645566899737751L;

            @Override
            protected Iterator<IModel<MenuItem>> getItemModels() {
                return mi.getSubmenus().iterator();
            }

            @Override
            protected void populateItem(final Item<MenuItem> item) {
                if (item.getModelObject().isDivider()) {
                    item.add(AttributeModifier.replace("class", "divider"));
                    item.add(new WebMarkupContainer("header").setRenderBodyOnly(true));

                } else if (item.getModelObject().isHeader()) {
                    item.add(AttributeModifier.replace("class", "dropdown-header"));
                    item.add(new Label("header", item.getModelObject().getLnkLabel()).setRenderBodyOnly(true));

                } else {
                    if (item.getModelObject().getClazz() != null) {
                        item.add(new MenuItemPanel("panel", item.getModelObject()));

                    } else {
                        item.add(new MenuItemSubmenuPanel("panel", item.getModelObject(), paginaAtiva));
                    }
                    item.add(new WebMarkupContainer("header").setRenderBodyOnly(true));
                    if (item.getModelObject().isUmDosSubMenusAtivos(paginaAtiva)) {
                        item.add(AttributeModifier.replace("class", "active"));
                    }
                }
            }
        };

        add(view);
    }

}
